//
//  RegistViewController.m
//  吃货·上海
//
//  Created by 高青松 on 15/11/25.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "RegistViewController.h"
#import "RFViewController.h"// 快速注册界面
#import <BmobSDK/Bmob.h>
@interface RegistViewController ()

@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UITextField *reNameTF;
@property (nonatomic,strong) UITextField *rePWTF;
@property (nonatomic,strong) UITextField *reCPWTF;
@property (nonatomic,strong) UITextField *reEmailTF;
@property (nonatomic,strong) UITextField *rePhoneTF;

@property (nonatomic,strong) UIButton *registBtn;
@property (nonatomic,strong) UIButton *cancelBtn;


@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [imageView setImage:[UIImage imageNamed:@"ch"]];
    [self.view addSubview:imageView];
    
    UIImageView *shaImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    shaImageView.backgroundColor = [UIColor blackColor];
    shaImageView.alpha = 0.3;
    [self.view addSubview:shaImageView];
    NSArray *array = @[@"用户名*",@"密码*",@"确认密码*",@"E-mail*",@"手机*"];
    for (int i = 0; i < 5; i++) {
        self.label = [[UILabel alloc]initWithFrame:CGRectMake((KScreenWidth - 300)/2, KScreenHeight/4 + 40 * i, 100, 30)];
        self.label.text = array[i];
        self.label.textColor = [UIColor blackColor];
        self.label.layer.masksToBounds = YES;
        [self.label.layer setCornerRadius:10];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.font = [UIFont boldSystemFontOfSize:16];
        self.label.backgroundColor = KColor;
        [self.view addSubview:self.label];
    }
    
    // 用户名
    self.reNameTF = [[UITextField alloc]initWithFrame:CGRectMake((KScreenWidth - 300)/2 + 100, KScreenHeight/4, 200, 30)];
    self.reNameTF.placeholder = @"请输入用户名";
    self.reNameTF.clearsOnBeginEditing = YES;
    self.reNameTF.backgroundColor = KMyColor;
    self.reNameTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.reNameTF];
    // 密码
    self.rePWTF = [[UITextField alloc]initWithFrame:CGRectMake((KScreenWidth - 300)/2 + 100, KScreenHeight/4 + 40, 200, 30)];
    self.rePWTF.placeholder = @"请输入密码";
    self.rePWTF.backgroundColor = KMyColor;
    self.rePWTF.secureTextEntry = YES;
    self.rePWTF.clearsOnBeginEditing = YES;
    self.rePWTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.rePWTF];
    // 确认密码
    self.reCPWTF = [[UITextField alloc]initWithFrame:CGRectMake((KScreenWidth - 300)/2 + 100, KScreenHeight/4 + 80, 200, 30)];
    self.reCPWTF.placeholder = @"请再次确认密码";
    self.reCPWTF.backgroundColor = KMyColor;
    self.reCPWTF.secureTextEntry = YES;
    self.reCPWTF.clearsOnBeginEditing = YES;
    self.reCPWTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.reCPWTF];
    
    // E-mail
    self.reEmailTF = [[UITextField alloc]initWithFrame:CGRectMake((KScreenWidth - 300)/2 + 100, KScreenHeight/4 + 120, 200, 30)];
    self.reEmailTF.placeholder = @"请输入E-mail";
    self.reEmailTF.backgroundColor = KMyColor;
    self.reEmailTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.reEmailTF];
    
    // phone
    self.rePhoneTF = [[UITextField alloc]initWithFrame:CGRectMake((KScreenWidth - 300)/2 + 100, KScreenHeight/4 + 160, 200, 30)];
    self.rePhoneTF.placeholder = @"请输入手机号";
    self.rePhoneTF.backgroundColor = KMyColor;
    self.rePhoneTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.rePhoneTF];
    
    // 注册 取消 快速注册
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame = CGRectMake(KScreenWidth/4, KScreenHeight/4 + 220, KScreenWidth/4-10, 30);
    self.cancelBtn.layer.masksToBounds = YES;
    [self.cancelBtn.layer setCornerRadius:8];
    self.cancelBtn.backgroundColor = [UIColor greenColor];
    [self.cancelBtn setTintColor:[UIColor blueColor]];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(cancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.cancelBtn];
    
    
    
    self.registBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.registBtn.frame = CGRectMake(KScreenWidth/2+10, KScreenHeight/4 + 220, KScreenWidth/4-10, 30);
    self.registBtn.backgroundColor = [UIColor greenColor];
    self.registBtn.layer.masksToBounds = YES;
    [self.registBtn.layer setCornerRadius:8];
    [self.registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.registBtn addTarget:self action:@selector(registBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registBtn];
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"快速注册" style:UIBarButtonItemStyleDone target:self action:@selector(rightbtn:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    UILabel *zhuLabel = [[UILabel alloc]initWithFrame:CGRectMake((KScreenWidth -150)/2, KScreenHeight - 50 - 64, 150, 30)];
    zhuLabel.text = @"注意:带*为必填内容";
    zhuLabel.textAlignment  = NSTextAlignmentCenter;
    zhuLabel.textColor = [UIColor redColor];
    [self.view addSubview:zhuLabel];
}

// 快速注册
- (void)rightbtn:(UIBarButtonItem *)barButton
{
    RFViewController *reVc = [[RFViewController alloc]init];
    [self.navigationController pushViewController:reVc animated:NO];
}


// 取消 确定注册
- (void)registBtn:(UIButton *)btn
{
    // 用户名长度范围
    if (self.reNameTF.text.length < 4 || self.reNameTF.text.length > 10) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您输入的用户名不符合要求,请重新输入" delegate:nil
                                                 cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    // 密码范围 和 密码确定
    if (self.rePWTF.text.length < 6 ) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您输入的密码过短,请重新输入" delegate:nil
                                                 cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    if (![self.rePWTF.text isEqualToString:self.reCPWTF.text]) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您输入的两次密码不同,请重新输入" delegate:nil
                                                 cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    if (self.reNameTF.text == nil || self.rePWTF.text == nil || self.reCPWTF.text == nil || self.reEmailTF.text == nil || self.rePhoneTF.text == nil) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您输入信息不完成,请补充完整" delegate:nil
                                                 cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    // 注册成功  绑定User
        //往_User表添加验证通过的手机用户
        BmobUser *bUser = [[BmobUser alloc]init];
        [bUser setUsername:self.reNameTF.text];
        [bUser setPassword:self.rePWTF.text];
        [bUser setEmail:self.reEmailTF.text];
        [bUser setMobilePhoneNumber:self.rePhoneTF.text];
//        [BmobUser loginWithUsernameInBackground:self.reNameTF.text
//                                       password:self.rePWTF.text];
        [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful)
            {
                NSLog(@"成功");
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功,请登陆" delegate:nil
                                                         cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
            else
            {
                NSLog(@"error =%@",error.description);                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"邮箱或者手机号格式错误" delegate:nil
                                                                                                               cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
        }];
    

}


- (void)cancelBtn:(UIButton *)btn
{
   [self.navigationController popViewControllerAnimated:NO];
}
// 回收键盘
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
    for (UIView *view in self.view.subviews) {
        [view resignFirstResponder];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
